<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>

<body>
    <div id="app">
        <input type="text" v-model="firstName"><br>
        <input type="text" v-model="lastName"><br>
        <input type="text" v-model="fullName"><br>
    </div>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                firstName: 'a',
                lastName: 'b',
                fullName: 'a-b',
                obj: {
                    name: 'houbang',
                    age: 21
                }
            }
        })
        vm.$watch('firstName', function (newValue, oldValue) {
            // console.log(newValue, oldValue);
            // console.log(this);
            this.fullName = newValue + '-' + this.lastName;
        })
        vm.$watch('lastName', function (newValue, oldValue) {
            // console.log(newValue, oldValue);
            // console.log(this);
            this.fullName = this.firstName + '-' + newValue;
        })
        vm.$watch('fullName', (newValue, oldValue) => {
            console.log(newValue, oldValue);
            let arr = newValue.split('-');
            console.log(arr);
            // 箭头函数的this指向当前函数的外层函数的this指向，此处为window
            console.log(this);

            vm.firstName = arr[0];
            vm.lastName = arr[1]
        })
        vm.$watch('obj', function (newValue, oldValue) {
            console.log(newValue, oldValue);
        }, {
            deep: true,
            immediate: true
        });
    </script>
</body>

</html>